


import 'package:get/get.dart';
import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/pages/common_page/common_dialog_page.dart';
import 'package:moonplay/pages/common_page/common_text_page.dart';
import 'package:flutter/material.dart';
/**历史记录 */

class HistoryPage extends StatefulWidget {
  @override
  _HistoryPageState createState() => _HistoryPageState();
}

class _HistoryPageState extends State<HistoryPage> {
  List historyRecord = SpUtil.getObjectList("historyRecord");

  @override
  void initState() {
    super.initState();
    LogUtil.e(historyRecord);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: commonAppbar(context, title: '历史记录', listActions: _listActions()),
      body: historyRecord != null && historyRecord.length > 0 ? bodyWidget() : Center(
        child: Cmtext('暂无记录', style: TextStyle(color: Color(0xff888888), fontSize: 15),),
      ),
    );
  }

  List<Widget> _listActions (){
    List<Widget> _list = [
      GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: (){
          CmUtils.openDialog(
            subTitle: '确定删除所有记录?',
            type: 2,
            confirmFun: (){
              Navigator.pop(context);
              SpUtil.putObjectList("historyRecord", []);
              setState(() {
                historyRecord = [];
              });
            }
          );
        },
        child: Container(
          width: ScreenUtil().getWidthPx(160),
          height: ScreenUtil().getWidthPx(100),
          alignment: Alignment.center,
          color: Colors.transparent,
          child: Cmtext('全部删除', style: TextStyle(fontSize: 14, color: Colors.white),),
        ),
      )
    ];
    return _list;
  }

  Widget bodyWidget(){
    return ListView.builder(
      itemCount: historyRecord.length,
      itemBuilder: (context, index) {
        return Container(
          height: ScreenUtil().getWidthPx(360),
          padding: EdgeInsets.symmetric(vertical: ScreenUtil().getWidthPx(30), horizontal: ScreenUtil().getWidthPx(30)),
          child: Row(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(2),
                    child: loadNetworkImage("${historyRecord[index]['vodPic']}"),
                  ),
                ),
              ),
              Expanded(
                flex: 2,
                child: Container(
                  margin: EdgeInsets.only(left: ScreenUtil().getWidthPx(20)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Expanded(
                        flex: 4,
                        child: Container(
                          child: Column(
                            children: <Widget>[
                              Container(
                                height: ScreenUtil().getWidthPx(60),
                                alignment: Alignment.centerLeft,
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Container(
                                      width: ScreenUtil().getWidthPx(320),
                                      child: Cmtext('${historyRecord[index]['vodName']}', style: TextStyle(color: Color(0xff009dff)), overflow: TextOverflow.ellipsis,),
                                    ),
                                    InkWell(
                                      onTap: (){
                                        CmUtils.openDialog(
                                          subTitle: '确定删除此条记录?',
                                          type: 2,
                                          confirmFun: (){
                                            Navigator.pop(context);
                                            historyRecord.removeAt(index);
                                            SpUtil.putObjectList("historyRecord", historyRecord);
                                            setState(() {});
                                          }
                                        );
                                      },
                                      child: Container(
                                        width: ScreenUtil().getWidthPx(60),
                                        height: ScreenUtil().getWidthPx(60),
                                        alignment: Alignment.center,
                                        child: CommonStyle.icons(0xe677, iconSize: 20),
                                      ),
                                    )
                                  ],
                                )
                              ),
                              Container(
                                height: ScreenUtil().getWidthPx(50),
                                alignment: Alignment.centerLeft,
                                child: Cmtext('${historyRecord[index]['vodArea']}', style: TextStyle(color: Color(0xff969696), fontSize: 13),),
                              ),
                              Container(
                                constraints: BoxConstraints(
                                  minHeight: ScreenUtil().getWidthPx(50),
                                ),
                                alignment: Alignment.centerLeft,
                                child: Cmtext('${historyRecord[index]['vodActor']}', style: TextStyle(color: Color(0xff969696), fontSize: 13),overflow: TextOverflow.ellipsis,maxLines: 2),
                              ),
                              Container(
                                height: ScreenUtil().getWidthPx(50),
                                alignment: Alignment.centerLeft,
                                child: Row(
                                  children: <Widget>[
                                    Cmtext('上次观看到第', style: TextStyle(color: Color(0xff969696), fontSize: 13),),
                                    Cmtext('${historyRecord[index]['setNum']}', style: TextStyle(color: Colors.red, fontSize: 13),),
                                    Cmtext('集', style: TextStyle(color: Color(0xff969696), fontSize: 13),),
                                    Cmtext('${historyRecord[index]['min']}', style: TextStyle(color: Colors.red, fontSize: 13),),
                                    Cmtext('分钟', style: TextStyle(color: Color(0xff969696), fontSize: 13),),
                                  ],
                                )
                              ),
                            ],
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Container(
                          height: ScreenUtil().getWidthPx(100),
                          child: Row(
                            children: <Widget>[
                              GestureDetector(
                                onTap: (){
                                  int _min = int.parse(historyRecord[index]['min']);
                                  int _sec = int.parse(historyRecord[index]['second']);
                                  int _videoVolume = (_min*60 + _sec) * 1000;
                                  Get.toNamed('/VideoDetailPage', arguments: {"vodId": historyRecord[index]['vodId'], "typeId1": historyRecord[index]['typeId1'] ,"videoVolume": _videoVolume, "setNum": historyRecord[index]['setNum']});
                                },
                                child: Container(
                                  width: ScreenUtil().getWidthPx(160),
                                  height: ScreenUtil().getWidthPx(80),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(20)),
                                    gradient:new LinearGradient(//背景渐变颜色，image 属性为 null 才有用
                                      begin: const Alignment(0.0, -1.0),
                                      end: const Alignment(0.0, 0.6),
                                      colors: <Color>[
                                        const Color(0xff08a0fe),
                                        const Color(0xff5bc4f4)
                                      ],
                                    ),
                                  ),
                                  child: Cmtext('继续观看', style: TextStyle(fontSize: 12, color: Colors.white),),
                                ),
                              )
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  confirmFun(index){
    historyRecord.removeAt(index);
    SpUtil.putObjectList("historyRecord", historyRecord);
    setState(() {});
  }
}